package com.example.homework3

import android.content.Intent
import android.os.Bundle
import android.widget.Button
import android.widget.EditText
import android.widget.TextView
import androidx.activity.enableEdgeToEdge
import androidx.appcompat.app.AppCompatActivity
import androidx.core.view.ViewCompat
import androidx.core.view.WindowInsetsCompat

const val FIRST_MSG="FIRST MSG"
const val SECOND_MSG="SECOND MSG"
class MainActivity : AppCompatActivity() {
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        enableEdgeToEdge()
        setContentView(R.layout.activity_main)
        ViewCompat.setOnApplyWindowInsetsListener(findViewById(R.id.main)) { v, insets ->
            val systemBars = insets.getInsets(WindowInsetsCompat.Type.systemBars())
            v.setPadding(systemBars.left, systemBars.top, systemBars.right, systemBars.bottom)
            insets
        }

        val button1=findViewById<Button>(R.id.button1)
        val editText=findViewById<EditText>(R.id.editTextText)
        button1.setOnClickListener{
            val intent = Intent(this, SecondActivity::class.java)
            intent.putExtra(FIRST_MSG,editText.text.toString())
            startActivity(intent)
        }

        val button2=findViewById<Button>(R.id.button2)
        button2.setOnClickListener{
            val intent =Intent(this,ThirdActivity::class.java)
            startActivityForResult(intent,0)
        }

        val button3=findViewById<Button>(R.id.button3)
        button3.setOnClickListener {
            val intent= Intent(this, FourthActivity::class.java)
            startActivity(intent)
        }

        val button4=findViewById<Button>(R.id.button4)
        button4.setOnClickListener {
            val intent= Intent(this, FifthActivity::class.java)
            startActivity(intent)
        }
    }

    override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
        super.onActivityResult(requestCode, resultCode, data)
        if(requestCode==0){
            if(resultCode==1){
                val textView=findViewById<TextView>(R.id.textView2)
                val receivedData=data?.getStringExtra(SECOND_MSG)
                textView.text=receivedData ?:"没有收到数据"
            }
        }
    }
}